package com.yh.controller;

import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.yh.bean.Rules;
import com.yh.service.RulesService;

@Controller
public class RulesController {
	
	@Autowired
	@Qualifier("rulesImp")
	private RulesService rs;
	    
	
	
	@ResponseBody
	@RequestMapping("/addRules")
	//部门新增制度；
	public  String  addRules(HttpServletRequest request,@RequestParam("file") MultipartFile file,	 Rules rule
								) throws UnsupportedEncodingException  {
		//获取用户ID
		 HttpSession session=request.getSession();
		 int user_id=Integer.parseInt((String) session.getAttribute("uId"));
		//定义编码
		request.setCharacterEncoding("UTF-8");
		//获取储存容器路径
		String   folderpath=request.getServletContext().getRealPath("rulesFile");
		//获取上传文件名称
		String  filename=file.getOriginalFilename();
		if(filename.endsWith(".txt")){
		//获取文件真是路径
		String realPath=folderpath+"/"+filename;
		//文件转存
		try {
			FileUtils.copyInputStreamToFile(file.getInputStream(), new File(realPath));
			DateFormat df=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			rule.setR_date(df.format(new Date()));
			rule.setR_filename(filename);
			rule.setR_uid(user_id);
			rule.setR_addr(realPath);	
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		if(rs.addRules(rule)==1){
			return "1";
		};
		}
		
		return "0";
		
	}

	@ResponseBody
	@RequestMapping("/updateRules")
	//部门新增制度；
	public  String  updateRules(HttpServletRequest req,Rules rule){
		if(rs.updateRules(rule)==1){
			return "1";
		}
		
		return "0";
	}
	
	@ResponseBody
	@RequestMapping("/deleteRules")
	//部门新增制度；
	public  String  deleteRules(Rules rule){
		if(rs.deleteRules(rule)==1){
			return "1";
		}
		
		return "0";
	}
	
	
	@ResponseBody
	@RequestMapping("/deleteArrayRules")
	//部门新增制度；
	public  String  deleteArrayRules(@RequestParam("array[]") int [] array){
		for (int i = 0; i < array.length; i++) {
			System.out.println(i);
		}
		if(rs.deleteArrayRules(array)==1){
			return "1";
		}
		
		return "0";
	}
	
	
	@ResponseBody
	@RequestMapping("/queryRules")
	//部门新增制度；
	public  Map  queryRules(Rules rule,int curr){
		System.out.println(rule.toString());
		return rs.queryRules(rule,curr);
	}
	
	
	
	
	//文件下载响应
	
	@RequestMapping("/downFile")
	public String  downFile(HttpServletResponse res,String fileName,String r_addr){
		//从response中获取输出流
		try {
			
			res.setCharacterEncoding("UTF-8");
			res.setContentType("multipart/form-data");
			System.out.println(fileName);
			fileName=new String(fileName.getBytes(), "ISO-8859-1");//防止下载时显示的文件名乱码
			res.setHeader("Content-Disposition","attachment;fileName="+fileName);
			OutputStream op=res.getOutputStream();
			op.write(FileUtils.readFileToByteArray(new File(r_addr)));
	
		} catch (IOException e) {
		   System.out.println("制度下载失败");
			e.printStackTrace();
		}
		
		
		return null;
	}
	
	
	
	

}
